<template>
  <section>
  <el-dialog
    title="人员信息导入"
    width="50%"
    :visible.sync="dialogVisible"
  >
    <el-form ref="form" :model="form" label-width="140px">
      <el-form-item label="模板" prop="name">
        <el-button size="small" type="primary" @click="downloadFile"
          >下载模板</el-button>
      </el-form-item>
      <el-form-item label="上传文件">
        <el-upload
          ref="upload"
          class="upload-demo"
          action=""
          :limit="1"
          :http-request="httpRequest"
          :on-change="handleChange"
          :on-remove="handleRemove"
          :auto-upload="false"
          accept=".xlsx,.xls"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传.xlsx,.xls结尾的文件
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="模板填写注意事项">
        <span>
        如果人员所在部门的名称在系统中是唯一的，只填写所在部门即可导入成功，但如果所在部门的名称不唯一，如部门领导、分局领导等，需要在所在单位中填写上级机构名称。
        </span>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确定</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </span>
  </el-dialog>
  </section>
</template>

<script>

import {importFile, importFileNew} from "@/api/modules/person";

export default {
  name: "FileImport",
  data() {
    return {
      form: {
        used: "0",
        typeName: undefined,
        typeId: undefined,
        standardTypeId: null,
        standardTypeName: null
      },
      dialogVisible: false,
      isGetOpen: false,
      clickOrgId: '',
    };
  },
  methods: {
    fileImportShow(orgId) {
      this.clickOrgId = orgId
      this.dialogVisible = true
    },
    downloadFile() {
      var urlStr = "/templateFile/人员信息模板.xlsx"
      window.location.href = urlStr
    },
    handleRemove(file) {
      this.form.file = undefined
    },
    handleChange(file) {
      this.form.file = file.raw
    },
    submitForm() {
      const files = this.$refs.upload.uploadFiles
      if (files.length < 1) {
        this.$message.success("请选择一个文件")
      } else {
        this.$refs.upload.submit()
      }
    },
    httpRequest(param) {
      const fileObj = param.file; // 相当于input里取得的files
      const data = new FormData(); // FormData 对象
      data.append("file", fileObj); // 文件对象
      importFileNew(data,this.clickOrgId).then((res) => {
        this.$message.success("成功导入");
        this.dialogVisible = false;
        this.$emit("file-import");
        this.$refs.upload.clearFiles();
      });
    },
  },
  created() {
  },
};
</script>

<style lang="scss" scoped></style>
